/*!
 * 基础js,所有页面都需要引入此js,依赖layer.js
 * shuliangxing
 */
(function($) {
	/*
	 *  jquery中各个事件执行顺序如下：
		1.ajaxStart(全局事件)
		2.beforeSend
		3.ajaxSend(全局事件)
		4.success
		5.ajaxSuccess(全局事件)
		6.error
		7.ajaxError (全局事件)
		8.complete
		9.ajaxComplete(全局事件)
		10.ajaxStop(全局事件)
	 */
	$(document).ajaxStart(onStart)
				.ajaxSend(onSend)
				.ajaxSuccess(onSuccess)
				.ajaxError(onError)
				.ajaxComplete(onComplete)
				.ajaxStop(onStop);

	function onStart(event) {
		//console.log("onStart"," | ",event);
		
		var index = top.layer.load(0, {
		    shade: [0.1,'#fff'] //0.1透明度的白色背景
		});
	}
	
	function onSend(event, xhr, options) {
		//console.log("onSend"," | ",event," | ",xhr," | ",options);
	}
	
	function onSuccess(event, xhr, options) {
		//console.log("onSuccess"," | ",event," | ",xhr," | ",options);
		//关闭所有弹框防止重叠
		//layer.closeAll();
		//top.layer.closeAll();
		top.layer.closeAll('loading');
	}
	
	function onError(event, xhr, options, exc) {
		//console.log("onError"," | ",event," | ",xhr," | ",options," | ",exc);
		//关闭所有弹框防止重叠
		layer.closeAll();
		top.layer.closeAll();
		
		//未授权
		if(xhr.status == 401){
			var msg = "对不起，请求未授权 状态码：" + xhr.status;
			var url = getContextPath() + "/";
			if(xhr.responseText){
				msg = xhr.responseText;
			}
			top.layer.alert(msg,{closeBtn:false},function(){
       	 		top.location.href=url;
	        });
        }
		//强制踢出登录
		else if(xhr.status == 901){
			var msg = "对不起，您被踢出登录 状态码：" + xhr.status;
			var url = getContextPath() + "/";
			if(xhr.responseText){
				var json = JSON.parse(xhr.responseText);
				if(json.msg){
					msg = json.msg;
				}
				if(json.url){
					url = getContextPath() + json.url;
				}
			}
			top.layer.alert(msg,{closeBtn:false},function(){
	    		 top.location.href=url;
		    });
		}else{
			var msg = "对不起，系统异常 状态码：" + xhr.status;
			if(exc == "timeout"){
				msg = "对不起，请求超时";
			}
			
			if(xhr.responseText){
				msg = xhr.responseText;
			}
			top.layer.alert(msg);
		}
	}
	
	function onComplete(event, xhr, options) {
		//console.log("onComplete"," | ",event," | ",xhr," | ",options);
		top.layer.closeAll('loading');
	}
	
	function onStop(event) {
		//console.log("onStop"," | ",event);
		top.layer.closeAll('loading');
	}
	
	/**
	 * 获取ContextPath
	 */
	function getContextPath() {
	    var pathName = document.location.pathname;
	    var index = pathName.substr(1).indexOf("/");
	    var result = pathName.substr(0,index+1);
	    return result;
	}
	
	/**
	 * 将表单转化为json对象
	 */
	$.fn.serializeJson=function(){
		var serializeObj={};
		var array=this.serializeArray();
		var str=this.serialize();
		$(array).each(function(){
			if(serializeObj[this.name]){
				if($.isArray(serializeObj[this.name])){
					serializeObj[this.name].push(this.value);
				}else{
					serializeObj[this.name]=[serializeObj[this.name],this.value];
				}
			}else{
				serializeObj[this.name]=this.value;	
			}
		});
		return serializeObj;
	};
}(jQuery));
